package com.situ.mall.web.controller;

import com.situ.mall.common.entity.Cart;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.exception.NoLoginException;
import com.situ.mall.common.service.ICartService;
import com.situ.mall.common.service.IMemberAddressService;
import com.situ.mall.common.service.IOrderDetailService;
import com.situ.mall.common.util.RequestUtil;

import com.situ.mall.common.vo.AllPriceVO;
import com.situ.mall.common.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/")
public class PayController {

    @Autowired
    private ICartService cartService;
    @Autowired
    private IMemberAddressService memberAddressService;
    @Autowired
    private IOrderDetailService orderDetailService;

    @PostMapping("/pay")
    public String Pay(String ids, Model model) {

        List<CartVO> vo = new ArrayList<>();
        for (String cId : ids.split(",")) {
            CartVO vo1 = cartService.select1(RequestUtil.getCurrentMemberId(), Integer.parseInt(cId));
            BigDecimal count = new BigDecimal(vo1.getAmount());
            vo1.setTotalPrice(vo1.getPrice().multiply(count));
            vo.add(vo1);
        }

        AllPriceVO sum = orderDetailService.allPrice(ids);
        model.addAttribute("sum", sum);
        model.addAttribute("cartList", vo);
        model.addAttribute("addressList", memberAddressService.select(RequestUtil.getCurrentMemberId()));


        return "pay";

    }

    @GetMapping("/pay/buyNow/{id}")
    public String buyNow(Model model, @PathVariable String id) {

        Member m = (Member) RequestUtil.getCurrent();
        if(m == null){
            throw new NoLoginException();
        }else {
            Cart cart = cartService.selectByPId(Integer.parseInt(id), m.getId());

            if (cart == null) {
                Cart c = new Cart();
                c.setProductId(Integer.parseInt(id));
                c.setCreateTime(LocalDateTime.now());
                c.setMemberId(m.getId());
                c.setAmount(1);
                cartService.saveOrUpdate(c);

            } else {
                cart.setAmount(cart.getAmount() + 1);
                cartService.saveOrUpdate(cart);
            }
            List<CartVO> vo = cartService.select2(m.getId(), Integer.parseInt(id));
            BigDecimal count = new BigDecimal(vo.get(0).getAmount());
            vo.get(0).setTotalPrice(vo.get(0).getPrice().multiply(count));
            AllPriceVO sum = orderDetailService.onePrice(Integer.parseInt(id));
            model.addAttribute("sum", sum);
            model.addAttribute("cartList", vo);
            model.addAttribute("addressList", memberAddressService.select(RequestUtil.getCurrentMemberId()));
        }

        return "pay";

    }


//    @PostMapping("/pay/buyNow/")
//    public String buyNow(Model model, @PathVariable String id) {
//
//        Member m = (Member) RequestUtil.getCurrent();
//        if(m == null){
//            throw new NoLoginException();
//        }else {
//            Cart cart = cartService.selectByPId(Integer.parseInt(id), m.getId());
//
//            if (cart == null) {
//                Cart c = new Cart();
//                c.setProductId(Integer.parseInt(id));
//                c.setCreateTime(LocalDateTime.now());
//                c.setMemberId(m.getId());
//                c.setAmount(1);
//                cartService.saveOrUpdate(c);
//
//            } else {
//                cart.setAmount(cart.getAmount() + 1);
//                cartService.saveOrUpdate(cart);
//            }
//            List<CartVO> vo = cartService.select2(m.getId(), Integer.parseInt(id));
//            BigDecimal count = new BigDecimal(vo.get(0).getAmount());
//            vo.get(0).setTotalPrice(vo.get(0).getPrice().multiply(count));
//            AllPriceVO sum = orderDetailService.onePrice(Integer.parseInt(id));
//            model.addAttribute("sum", sum);
//            model.addAttribute("cartList", vo);
//            model.addAttribute("addressList", memberAddressService.select(RequestUtil.getCurrentMemberId()));
//        }
//
//        return "pay";
//
//    }


}
